<?php

namespace app\api\controller;

use think\Controller;
use think\Db;

class Articles extends Father
{
    //页面分页
    public function page($page ,$size,$success,$message){
        $return = [
            'data' => '',
            'success' => $success,
            'messages' => $message,
            'page' =>$page,
            'size' => $size
        ];
        $data2 = model('Articles')->where('level',2)->order('weight asc,update_time desc')->paginate($size, false, [
            'page' => $page,
            'var_page' => 'page'
        ]);
        $data = $data2->toArray();

        if ($page>$data['last_page']) {
            $data = model('Articles')->where('level',2)->order('weight asc,update_time desc')->paginate($size, false, [
                'page' => $data['last_page'],
                'var_page' => 'page'
            ]);
            $data = $data->toArray();
            $return['page'] = $data['last_page'];
        }
        $article = $data['data'];
        foreach ($article as $k=>$v){
            $article[$k]['homeShow'] =json_decode($v['homeShow']);
              $class =  Db::table('classes')->where('id', $article[$k]['homeShow'])->field('name')->find();
            $article[$k]['class'] =$class ;
            $article[$k]['listStyle'] =json_decode($v['listStyle']);
            $article[$k]['file'] =json_decode($v['file']);
            $article[$k]['releaseAt'] = date('Y-m-d H:i:s', $article[$k]['releaseAt']);
            $article[$k]['tag']= explode(",",$article[$k]['tag']);

        }
        $return['data'] = $article;
        $return['total'] = $data['total'];

        $return['page'] = $data['current_page'];
        return $return;
    }
    //热门推荐
    public function Hot(){
     $page= input('get.page');
        $size= input('get.size');
        $return = $this->page($page,$size,true,"查询成功");
        return json($return);


    }
    //文章列表
    public function articleList(){


        $class = input('get.class');
        $page = input('get.page');
        $size = input('get.size');
        $keywords = input('get.search');
        $return = [
            'data'=>'',
            'message'=> "查询失败",
            'success' => false,
            'page' =>$page,
            'size' =>$size,
            'total'=>'',
             ];

        if ($keywords==''||$keywords==null){
            $data = model('Articles')->where('class_id',$class)->order('weight asc,update_time desc')->paginate($size,false,[

                'var_page'=>'page'
            ]);

        }else{
            $data = model('Articles')->where('class_id',$class)->where('title|content','like','%'.$keywords.'%')->order('weight asc,update_time desc')->paginate($size,false,[

                'var_page'=>'page'
            ]);

        }

           $data = $data->toArray();
        if ($data){
            $article = $data['data'];
            foreach ($article as $k=>$v){
                $article[$k]['homeShow'] =json_decode($v['homeShow']);

                $article[$k]['listStyle'] =json_decode($v['listStyle']);
                $article[$k]['file'] =json_decode($v['file']);
                $article[$k]['releaseAt'] = date('Y-m-d H:i:s', $article[$k]['releaseAt']);
                $article[$k]['tag']= explode(",",$article[$k]['tag']);

            }


            $return['data'] = $article;
            $return['message'] = '查询成功';
            $return['success'] = true;
            $return['total'] = $data['total'];
        }
        return json($return);

    }
    //轮播图
    public function lunbo(){
        $data = model('Articles')->field('id,homeShow,title,pageview,listStyle')->order('weight asc,update_time desc')->select();
        return json($data);

    }
    //文章详情
    public function contentArticles(){
        $json = [
            'data' => '',
            'success' => false,
            'messages' => '查询文章失败'
        ];

        $id = input('get.id');
        $ArticleInfo = model('Articles')->where('id',$id)->find();


        if ($ArticleInfo){
            $ArticleInfo['homeShow'] =json_decode($ArticleInfo['homeShow']);
            $ArticleInfo['listStyle'] =json_decode($ArticleInfo['listStyle']);
            $ArticleInfo['file'] =json_decode($ArticleInfo['file']);
            $ArticleInfo['tag']= explode(",",$ArticleInfo['tag']);
            $ArticleInfo['releaseAt'] = date('Y-m-d H:i:s',$ArticleInfo['releaseAt']);
            $json['data'] = $ArticleInfo;
            $json['success'] = true;
            $json['messages'] ='查询成功';

        }
        return json($json);
    }
}
